<template>
  <div class="mod-config">
    <basic-container>
      <div class="boxsty zhiBiao">
        <div class="flex justifyContentSpaceBetween">
          <div >
            <span><b>负责部门：{{ deptName }}</b></span>
          </div>
          <div class="tablelink">
            <el-link icon="el-icon-plus" v-if="permissions.jiangjinfpjjfpgrdxb_add" type="primary"
                     @click="addOrUpdateHandle()" :underline="false" :disabled="deptId===''">新增
            </el-link>
          </div>
        </div>
        <div>&nbsp;</div>

        <div style="height: 67vh; overflow-y: auto;">
          <el-table :data="dataList" v-loading="dataListLoading" stripe class="avue-crud" height="600px">
            <el-table-column prop="" label="序号" :formatter="nShowIndex" width="70"></el-table-column>
             <el-table-column prop="ryCode" label="人员编号"></el-table-column>
             <el-table-column prop="ryName" label="人员名称"></el-table-column>
             <el-table-column prop="jjxMc" label="奖金项名称"></el-table-column>
             <el-table-column prop="jjJe" label="奖金金额" :formatter="nFormatterShow"></el-table-column>
             <el-table-column label="操作" width="200">
                <template slot-scope="scope">
                  <el-button v-if="permissions.jiangjinfpjjfpgrdxb_edit" type="text" size="small" icon="el-icon-edit" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
                  <el-button v-if="permissions.jiangjinfpjjfpgrdxb_del" type="text" size="small" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
          </el-table>
        </div>

        <div class="avue-crud__pagination">
          <el-pagination
                  @size-change="sizeChangeHandle"
                  @current-change="currentChangeHandle"
                  :current-page="pageIndex"
                  :page-sizes="[10, 20, 50, 100]"
                  :page-size="pageSize"
                  :total="totalPage"
                  background
                  layout="total, sizes, prev, pager, next, jumper">
          </el-pagination>
        </div>
      </div>
      <!-- 弹窗, 新增 / 修改 -->
      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></table-form>
    </basic-container>
  </div>
</template>

<script>
  import {fetchList, delObj} from '@/api/jiangjin/jiangjinfpjjfpgrdxb'
  import TableForm from './jiangjinfpjjfpgrdxb-form'
  import {mapGetters} from 'vuex'
  import {getJiangJinBm} from '@/api/admin/dept'

  export default {
    data () {
      return {
        dataForm: {
          key: ''
        },

        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addOrUpdateVisible: false,
        deptId: '',//负责部门ID
        deptName: '',//负责部门名称
      }
    },
    components: {
      TableForm
    },
    created () {

    },
    mounted() {
      this.getJiangJinJg();
    },
    computed: {
      ...mapGetters(['permissions'])
    },
    methods: {
      //获取奖金管理人机构
      getJiangJinJg() {
        getJiangJinBm().then(res => {
          if (res.data.data != null && res.data.data.length > 0) {
            this.deptId = "";
            this.deptName = "";
            this.deptId = res.data.data[0].deptId;
            this.deptName = res.data.data[0].name;
            this.getDataList();
          } else {
            this.$notify.closeAll();
            //this.$notify.error("没有相关负责部门")
            //this.deptName = "无相关负责部门";
          }
        });
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        fetchList(Object.assign({
          current: this.pageIndex,
          size: this.pageSize
        })).then(response => {
          this.dataList = response.data.data.records
          this.totalPage = response.data.data.total
        })
        this.dataListLoading = false
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      nShowIndex(row, column, cellValue, index) {
        return index + 1;
      },
      //列显示重新定义
      nFormatterShow(row, column, cellValue, index){
        if(cellValue){
          return cellValue.toFixed(2);
        }else
        {
          return cellValue;
        }
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id,this.deptId)
        })
      },
      // 删除
      deleteHandle (id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return delObj(id)
        }).then(data => {
          this.$message.closeAll()
          this.$message.success('删除成功')
          this.getDataList()
        })
      }
    }
  }
</script>
